﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Entity;
using ASV.Classes;
using System.Data;
namespace ASV.Account
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         //   RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void registrierenButton_Click(object sender, EventArgs e)
        {
            string vorname = Vorname.Text;
            string nachname = Nachname.Text;
            string email = Email.Text;
            string passwort = Password.Text;
            string passwortRepeat = PasswordRepeat.Text;
            bool errorStatus = true;
            string errorText = "<ul>";

            if (this.checkEmail(email) == false)
            {
                errorText += "<li><b>Email</b>: Diese E-Mail wird bereits verwendet</li>";
                errorStatus = false;
            }

            if (this.checkPassword(passwort, passwortRepeat) == false)
            {
                errorText += "<li><b>Passwort</b>: Die Passwörter stimmen nicht überein</li>";
                errorStatus = false;
            }

            errorText +=  "</ul>";

            if (errorStatus == false)
            {
                errors.Text = errorText;
            }
            else
            {
                this.insertUser(vorname, nachname, email, passwort);
                registForm.Visible = false;
                erfolgsmeldung.Visible = true;

                object [] test = this.getKundennummer(email);
                string kundennummer = test[0].ToString();

                regist_vorname.Text = vorname;
                regist_nachname.Text = nachname;
                regist_kundennummer.Text = kundennummer;
            }
        }

        private bool checkPassword(string password, string passwordRepeat)
        {
            if (password != passwordRepeat)
            {
                return false;
            }
            return true;
        }

        private bool checkEmail(string email)
        {
            Connection connection = new Connection();
            connection.connectDB();

            MySqlCommand selectUser = new MySqlCommand();
            string SQL = "SELECT * FROM benutzer WHERE email = '" + email + "'";
            selectUser.CommandText = SQL;
            selectUser.Connection = connection.getConnection();

            MySqlDataAdapter adapter = new MySqlDataAdapter(selectUser);
            MySqlDataReader reader = selectUser.ExecuteReader();

            if (reader.HasRows == true)
            {
                return false;
            }

            return true;
        }

        private void insertUser(string vorname, string nachname, string email, string passwort)
        {
            Connection connection = new Connection();
            connection.connectDB();

            MySqlCommand insertCommand = new MySqlCommand("INSERT INTO `benutzer` (`vorname`,`nachname`,`email`,`passwort`,`benutzertyp`) VALUES ('" + vorname + "', '" + nachname + "', '" + email + "', '" + passwort + "', '0')", connection.getConnection());
            insertCommand.ExecuteNonQuery();
        }

        private object[] getKundennummer(string email)
        {
            Connection connection = new Connection();
            DataTable datatable;
            connection.connectDB();
            datatable = connection.select("SELECT * FROM benutzer WHERE `email` = '" + email + "' LIMIT 1");
            object[] Kunde = datatable.Rows[0].ItemArray;
            return Kunde;
        }
    }
}
